﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace iPower
{
    class DynamicButtons
    {
        private int posX;
        private int posY;
        private int sizeWidth;
        private int sizeHeight;
        private bool createdButtonFlag;
        private List<Button> listButtons = new List<Button>();

        // Constructor + Overloaded method to set properties 
        public DynamicButtons()
        {
            this.posX = 10;
            this.posY = 20;
            this.sizeWidth = 140;
            this.sizeHeight = 30;
        } //DynamicButtons()

        public DynamicButtons(int posX, int posY, int sizeWidth, int sizeHeight)
        {
            this.posX = posX;
            this.posY = posY;
            this.sizeWidth = sizeWidth;
            this.sizeHeight = sizeHeight;
        } //DynamicButtons()

        public void createButtons(int countButton, List<String> listButtonText, EventHandler eventhandlerButton)
        {
            if (listButtonText.Count <= countButton)
            {
                if (createdButtonFlag == true)
                {
                    foreach (Button button in listButtons)
                    {
                        button.Dispose();
                    }//foreach

                    // Clear List Container
                    listButtons.Clear();

                    // Set createButtonFlag to False = doesnt Run on next create cycle.
                    createdButtonFlag = false;
                }//if

                // Flag to indicate createButton called, if called remove Buttons first.
                createdButtonFlag = true;

                // Create Dynamic Buttons 
                for (int x = 0; x < countButton; x++)
                {
                    this.createButton();
                }//for

                foreach (Button button in listButtons)
                {
                    button.Text = listButtonText[listButtons.IndexOf(button)];
                } //foreach

                // Add EventHandler to Buttons Created
                foreach (Button button in listButtons)
                {
                    button.Click += new EventHandler(eventhandlerButton);
                } //foreach
            }//if
            else
            {
                MessageBox.Show("Dynamic Text List > than Dynamic Button Count","Dynamic Button Creation",
                                 MessageBoxButtons.OK, MessageBoxIcon.Error);
            }//else
        
        } //createButtons()

        public void deleteButtons(Control control, EventHandler eventhandlerButton)
        {
            foreach (Button button in listButtons)
            {
                control.Controls.Remove(button);
                button.Dispose();
                button.Click -= new EventHandler(eventhandlerButton);                
            }
            // Clear List button Container
            listButtons.Clear();
        } //deleteButtons()


        public void assignButtons(Control control)
        {
            foreach (Button button in listButtons)
            {
                control.Controls.Add(button);
            } //foreach
        } //assignButtons()

        public void clearColorButton()
        {
            foreach (Button button in listButtons)
                button.BackColor = Color.Transparent;
        }//clearColourButton()

        public void enableButtons()
        {
            foreach (Button button in listButtons)
            {
                button.Enabled = true;
            } //foreach

        } //enableButtons

        public void disableButtons()
        {
            foreach (Button button in listButtons)
            {
                button.Enabled = false;
            } //foreach

        } //disableButtons

        private void createButton()
        {
            Button dynButton = new Button();
            Point dynPoint = new Point(this.posX, this.posY);

            dynButton.Size = new Size(this.sizeWidth, this.sizeHeight);

            if (listButtons.Count() == 0)
            {
                dynButton.Location = dynPoint;
            } //if
            else
            {
                dynPoint.X = this.posX;
                dynPoint.Y = listButtons[listButtons.Count() - 1].Location.Y + (this.sizeHeight + 2); //review this, should be good
                dynButton.Location = dynPoint;
            } //else
            listButtons.Add(dynButton);
        } //createButton()
    } //class
} //namespace
        



